

package com.example.a14874.demoa;

import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btnQueryStu;
    private Button btnQuerySco;
    private TextView txtResult;
    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        btnQueryStu=findViewById( R.id.btn_query_stu );
        btnQuerySco=findViewById( R.id.btn_query_sco );
        txtResult=findViewById( R.id.txt_result );
        btnQueryStu.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if(cursor != null && cursor.getCount() > 0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString( 0 );
                        String name = cursor.getString( 1 );
                        int age = cursor.getInt( 2 );
                        String sex=cursor.getString( 3 );
                        buffer.append( stuno ).append( "  " ).append( name ).append( "  " ).append( age ).append( sex ).append( "\n" );
                    }
                    txtResult.setText( buffer );
                }
            }
        } );
        btnQuerySco.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if(cursor != null && cursor.getCount() > 0){
                    StringBuffer buffer = new StringBuffer();
                    for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                        String stuno = cursor.getString(0);
                        int english  = cursor.getInt(1);
                        int android = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(english).append("  ").append(android).append("\n");
                    }

                    txtResult.setText(buffer);
                }
            }
        } );
    }
}
